﻿namespace AntdUI_HamburgerMenuTabs.Forms
{
    public partial class LogViewer : AntdUI.Window
    {
        private readonly System.Windows.Forms.Timer _timer = new() { Interval = 200 };

        public LogViewer()
        {
            InitializeComponent();

            this.InitTheme();

            StartPosition = FormStartPosition.Manual;

            Shown += (s, e) =>
            {
                Left = 0;

                if (ParentForm != null)
                {
                    Top = Screen.FromPoint(ParentForm.Location).WorkingArea.Height - Height;
                }
                else
                {
                    Top = Screen.FromPoint(Cursor.Position).WorkingArea.Height - Height;
                }

                _timer.Start();
            };

            VisibleChanged += (s, e) =>
            {
                if (Visible)
                {
                    _timer.Start();
                }
                else
                {
                    _timer.Stop();
                }
            };

            Disposed += (s, e) =>
            {
                _timer.Stop();
                _timer.Dispose();
            };

            _timer.Tick += (s, e) =>
            {
                var log = LogExtentions.LogDequeue();
                if (log is not null)
                {
                    // 日志队列不为空时，提供颜色提示
                    if (btnPause.Type != AntdUI.TTypeMini.Success)
                        btnPause.Type = AntdUI.TTypeMini.Success;
                    if (btnPause.ToggleType != AntdUI.TTypeMini.Warn)
                        btnPause.ToggleType = AntdUI.TTypeMini.Warn;
                    Append(log);
                }
                else
                {
                    // 日志队列为空时，取消颜色提示
                    if (btnPause.Type != AntdUI.TTypeMini.Default)
                        btnPause.Type = AntdUI.TTypeMini.Default;
                    if (btnPause.ToggleType != AntdUI.TTypeMini.Default)
                        btnPause.ToggleType = AntdUI.TTypeMini.Default;
                }
            };

            tipB.SetTip(btnPause, "暂停滚动");
            btnPause.Click += (s, e) =>
            {
                btnPause.Toggle = !btnPause.Toggle;
                if (btnPause.Toggle)
                {
                    tipB.SetTip(btnPause, "开始滚动");
                }
                else
                {
                    tipB.SetTip(btnPause, "暂停滚动");
                }
            };

            btnClear.Click += (s, e) => Clear();

            btnDemo.Click += (s, e) =>
            {
                string batch = Guid.NewGuid().ToString().Replace("-", "");
                for (int i = 0; i < 100; i++)
                {
                    string log = $"Log - {batch} - {i + 1:0000}";
                    log.LogEnqueue();
                }
            };
        }

        private static LogViewer? _instence;
        public static LogViewer Instance
        {
            get
            {
                if (_instence == null || _instence.IsDisposed)
                {
                    _instence = new LogViewer();
                }
                return _instence;
            }
        }

        /// <summary>
        /// 全局快捷键
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Close();
                    break;
            }
            return false;
        }

        /// <summary>
        /// 清除日志
        /// </summary>
        public void Clear()
        {
            _timer.Stop();
            inputLog.Clear();
            _timer.Start();
        }

        /// <summary>
        /// 追加日志
        /// </summary>
        /// <param name="text"></param>
        public void Append(string text)
        {
            if (InvokeRequired)
            {
                Invoke(new Action<string>(AppendInternal), text);
            }
            else
            {
                AppendInternal(text);
            }
        }

        private void AppendInternal(string text)
        {
            if (inputLog.Text.Length > 0)
            {
                inputLog.AppendText(Environment.NewLine);
            }
            inputLog.AppendText($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.ffffff}] {text}");
            if (!btnPause.Toggle)
            {
                inputLog.ScrollToCaret();
                inputLog.SelectionStart = inputLog.Text.Length;
            }
        }
    }
}